package com.ray.life;

import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
//import android.text.util.Linkify;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class NewsDetailActivity extends Activity {
    private final String TAG = "NewsDetailActivity";

    private TextView mTitle;
//    private TextView mDesc;
//    private TextView mLink;
    private TextView mInfo;
    private TextView mDate;
    private TextView mAddr;

    private Button mMapBtn;
    private Button mWebBtn;
    private Button mRouteBtn;
    private Button mStreetBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "onCreate");

        setContentView(R.layout.news_content);

        mTitle = (TextView) findViewById(R.id.news_title);
//        mDesc = (TextView) findViewById(R.id.news_desc);
//        mLink = (TextView) findViewById(R.id.news_link);
        mInfo = (TextView) findViewById(R.id.news_info);
        mDate = (TextView) findViewById(R.id.news_date);
        mAddr = (TextView) findViewById(R.id.news_addr);

        mMapBtn = (Button) findViewById(R.id.map_button);
        mWebBtn = (Button) findViewById(R.id.web_button);
        mRouteBtn = (Button) findViewById(R.id.route_button);
        mStreetBtn = (Button) findViewById(R.id.street_button);

        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        String link = bundle.getString(Constants.KEY_LINK);
        String desc = bundle.getString(Constants.KEY_DESC);

        mTitle.setText(bundle.getString(Constants.KEY_TITLE));
//        mDesc.setText(Html.fromHtml(desc));
//        mLink.setText(link);

        ContentParser cp = new ContentParser(desc);
        cp.parse();

        mInfo.setText(Html.fromHtml(cp.getActInfo()));
        mDate.setText(cp.getActDuration());
        String addr = cp.getActAddr();
        mAddr.setText(addr);

        cp = null;

//        Linkify.addLinks(mLink, Linkify.WEB_URLS);

        int index = bundle.getInt(Constants.KEY_INDEX);
        setButtons(addr, link, index);
    }

    /** set buttons */
    private void setButtons(final String addr, final String link, final int index) {
        mMapBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                MapViewActivity.show(NewsDetailActivity.this, addr, index);
            }
        });

        mWebBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Uri uri = Uri.parse(link);  
                Intent it = new Intent(Intent.ACTION_VIEW, uri);  
                startActivity(it);  
            }
        });

        mRouteBtn.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                GeoPoint gpFrom;
                GeoPoint gpTo;
                if (!TextUtils.isEmpty(addr)) {
                    Location location = LocationHelper.getLocation((LocationManager) getSystemService(Context.LOCATION_SERVICE));
                    if (location == null) {
                        Log.e(TAG, "location is null");
                    }
                    gpFrom = new GeoPoint((int) (location.getLatitude() * LocationHelper.MILLION), (int) (location.getLongitude() * LocationHelper.MILLION));
                    gpTo = LocationHelper.getGeoPointByAddress(NewsDetailActivity.this, addr);

                    Intent intent = new Intent();
                    intent.setAction(android.content.Intent.ACTION_VIEW);

                    intent.setData(Uri.parse("http://maps.google.com/maps?f=d&saddr=" + LocationHelper.GeoPointToString(gpFrom)
                            + "&daddr=" + LocationHelper.GeoPointToString(gpTo) + "&hl=tw"));
                    startActivity(intent);
                }
            }
        });

        mStreetBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                GeoPoint gp = LocationHelper.getGeoPointByAddress(NewsDetailActivity.this, addr);
                String position = LocationHelper.GeoPointToString(gp);
                String uriString = "google.streetview:cbll=" + position + "&cbp=1,90,,0,1.0";
                Uri uri = Uri.parse(uriString);
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            }
        });
    }
}
